package com.bawei.module_service.ui.maintain

import android.text.TextUtils
import android.view.View
import android.widget.PopupMenu

import androidx.activity.viewModels

import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_service.R
import com.bawei.module_service.adapter.MaintainAdapter
import com.bawei.module_service.databinding.ActivityMaintainBinding
import com.bawei.module_service.http.ServiceViewModel
import com.bawei.module_service.model.MaintainModelItem
import com.bumptech.glide.Glide
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.MAINTAIN_PAGE)
class MaintainActivity :BaseActivity<ServiceViewModel,ActivityMaintainBinding> (){
    //
    override val mLayoutResId: Int
        get() = R.layout.activity_maintain

    val mAdapter: MaintainAdapter by lazy {
        MaintainAdapter()
    }
    var  pd:Boolean=false
   private val mViewModel: ServiceViewModel by viewModels()

    override fun initView() {
      super.initView()


        mBinding.maintainRv.adapter=mAdapter
        mBinding.maintainRv.layoutManager=LinearLayoutManager(this)

        mBinding.maintainToobar.more.visibility=View.GONE
        mBinding.maintainToobar.back.setOnClickListener {
            finish()
        }
        mBinding.maintainToobar.title1.text="维修管理"
        mBinding.maintainToobar.title.visibility=View.GONE
        mBinding.maintainToobar.title1.visibility=View.VISIBLE



        mBinding.shaixuan.setOnClickListener {
            showPopupMenu(it)
        }

        mBinding.filter.setOnClickListener {
            pd=!pd

            if (!pd){

                mAdapter.setData(mViewModel.mMaintainList)
            }else{
                mAdapter.setData(mViewModel.mMaintainList.reversed())
            }
         }


    }

    override fun initData() {
       mViewModel.getMaintainList()
    }

    fun showPopupMenu( view: View){
        val popupMenu: PopupMenu = PopupMenu(this,view )
        popupMenu.getMenuInflater().inflate(R.menu.popup_menu_items2, popupMenu.getMenu())
        popupMenu.setOnMenuItemClickListener { item ->
            when (item.itemId) {
                R.id.menu_item_1 -> {

                    var list= mutableListOf<MaintainModelItem>()
                    for (modelItem in mViewModel.mMaintainList){
                        if (modelItem.processingStatus=="已完成"){
                            list.add(modelItem)
                            mAdapter.setData(list)
                        }
                    }
                }
                 R.id.menu_item_2 -> {
                     var list= mutableListOf<MaintainModelItem>()
                     for (modelItem in mViewModel.mMaintainList){
                         if (modelItem.processingStatus=="处理中"){
                             list.add(modelItem)
                             mAdapter.setData(list)
                         }
                     }
                }
                R.id.menu_item_3 -> {
                    var list= mutableListOf<MaintainModelItem>()
                    for (modelItem in mViewModel.mMaintainList){
                        if (modelItem.processingStatus=="待处理"){
                            list.add(modelItem)
                            mAdapter.setData(list)
                        }
                    }
                }
            }
            true
        }
        popupMenu.show()

    }


    override fun initListener() {

        mBinding.floatingButton.setOnClickListener {
            ARouter.getInstance().build(Router.REPAIR_PAGE).navigation()
        }



        mBinding.maintainQuery.setOnQueryTextListener(
            object : SearchView.OnQueryTextListener {
                override fun onQueryTextSubmit(query: String): Boolean {
                    searchMaintainSpaces(query)
                    return true
                }

                override fun onQueryTextChange(newText: String): Boolean {
                    searchMaintainSpaces(newText)
                    return true

                    }

            }
        )

        mViewModel.mMaintainState.observeWithLifecycle(this){
            when(it){
                Empty -> {

                }
                is Failed ->{
                    ToastUtils.showLong(this, it.message)

                }
                Loading -> {

                }
                Success -> {

                    mAdapter.setData(mViewModel.mMaintainList)


                }
            }
        }


    }

    private fun searchMaintainSpaces(query: String) {

        if (TextUtils.isEmpty(query)){
            mAdapter.setData(mViewModel.mMaintainList)
        }else{
            mViewModel.mMaintainList_new.clear()
            var lowerCaseQuery=query.toLowerCase()
            for (modelItem in mViewModel.mMaintainList){
                if (modelItem.repairName.contains(lowerCaseQuery)){
                    mViewModel.mMaintainList_new.add(modelItem)
                }

            }
            mAdapter.setData(mViewModel.mMaintainList_new)

        }
    }


}
